package com.cskaoyan.service;


import com.cskaoyan.bean.*;
import com.cskaoyan.bo.OrderShipBO;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.Order_GoodsMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.OrderDetaliVO;
import com.cskaoyan.vo.OrderListVO;
import com.cskaoyan.vo.OrderVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    Order_GoodsMapper orderGoodsMapper;

    @Autowired
    UserMapper userMapper;

  //  @Override
  /*  public OrderVO queryOrder(Integer page, Integer limit, Integer[] orderStatusArray, String sort, Integer userId, String orderSn, String order) {

        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(101);
        arrayList.add(102);
        arrayList.add(103);
        arrayList.add(201);
        arrayList.add(301);
        arrayList.add(401);
        arrayList.add(402);
        PageHelper.startPage(page, limit);

        OrderExample orderExample = new OrderExample();

        orderExample.setOrderByClause(sort + " " + order);

        OrderExample.Criteria criteria = orderExample.createCriteria();

        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (orderSn != null && !"".equals(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }

        if ((orderStatusArray!=null) && !"".equals(orderStatusArray)) {

            for (int i = 0; i < orderStatusArray.length; i++) {
                for (Integer arr : arrayList) {
                    if (orderStatusArray[i] == arr) {
                        criteria.andOrderStatusEqualTo(arr);
                    }
                }
            }
        }


        List<Order> orders = orderMapper.selectByExample(orderExample);

        PageInfo<Order> orderPageInfo = new PageInfo<>();

        long total = orderPageInfo.getTotal();

        OrderVO orderVO = new OrderVO();

        orderVO.setTotal(total);

        orderVO.setItems(orders);

        return orderVO;
    }
*/
    @Override
    public OrderDetaliVO queryOrderDetail(Integer id) {
        //Order表
        Order order = orderMapper.selectByPrimaryKey(id);

        //Goods表
        Order_Goods orderGoods = orderGoodsMapper.selectByPrimaryKey(id);

        //用户表
        User user = userMapper.selectByPrimaryKey(order.getUserId());


        OrderDetaliVO orderDetaliVO = new OrderDetaliVO();

        orderDetaliVO.setOrder(order);
        orderDetaliVO.setOrderGoods(orderGoods);
        orderDetaliVO.setUser(user);
        return orderDetaliVO;
    }

    @Override
    public void ship(OrderShipBO orderShipBO) {
        Order order = new Order();
        order.setId(orderShipBO.getOrderId());
        order.setShipSn(orderShipBO.getShipSn());
        order.setShipChannel(orderShipBO.getShipChannel());
        order.setOrderStatus(Short.parseShort("301"));
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public OrderListVO getlist(Integer page, Integer limit, String sort, String order, Integer userId, Integer[] orderStatusArray, String orderSn) {
        //        用来存放最后的数据
        OrderListVO finalorderListVO = new OrderListVO();
        PageHelper.startPage(page,limit);
        PageHelper.startPage(page,limit);
        OrderExample orderExample = new OrderExample();
        orderExample.setOrderByClause(sort+" "+order);
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null && !"".equals(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        if (orderSn!=null && !"".equals(orderSn)){
            criteria.andOrderSnEqualTo(orderSn);
        }
        List<Order> orders = orderMapper.selectByExample(orderExample);
        if (orderStatusArray==null || "".equals(orderStatusArray)){
            finalorderListVO.setItems(orders);
            PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
            long total = orderPageInfo.getTotal();
            finalorderListVO.setTotal(total);
            return finalorderListVO;
        }
        ArrayList<Order> orderlist = new ArrayList<>();
        for (Order order1 : orders) {
            for (Integer integer : orderStatusArray) {
                if (order1.getOrderStatus().toString().equals(integer.toString())){
                    orderlist.add(order1);
                }
            }
        }
        PageInfo<Order> orderPageInfo = new PageInfo<>(orderlist);
        long total = orderPageInfo.getTotal();
        finalorderListVO.setTotal(total);
        finalorderListVO.setItems(orderlist);
        return finalorderListVO;
    }
}
